package com.etc.controller;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.CommonMessage;
import com.etc.util.PageData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("api")
public class UserCotroller {
    @Resource(name="userService")
    private UserService us;

    public String ttt(){
        return "";
    }
    /**
     *
     * @param page 第几页，jsp传过来
     * @param limit 页面显示的条数，jsp传过来
     * @param content 模糊查询的内容
     * @return
     */
    @RequestMapping(value="userPage")
    @ResponseBody
    public PageData<User> UserByPage(Integer page, Integer limit, String content){
        if(content==null)
            content="";
        PageData<User> pd=us.getUsersByPage(page, limit, content);
        pd.setCode(0);
        pd.setMsg("ok");
        return pd;
    }


    @RequestMapping(value="edit")
    @ResponseBody
    public CommonMessage UserEdit(User user){
        boolean flag=us.update(user);
        CommonMessage cm=new CommonMessage();
        if(flag) {
            cm.setMsg("修改成功");
        }else {
            cm.setMsg("修改失败");}
        return cm;

    }

    @RequestMapping(value="del")
    @ResponseBody
    public CommonMessage del(int userid){
        boolean flag=us.delUsers(userid);
        CommonMessage cm=new CommonMessage();
        if(flag) {
            cm.setMsg("删除成功");
        }else {
            cm.setMsg("删除失败");}
        return cm;
    }

    @RequestMapping(value="add")
    @ResponseBody
    public CommonMessage UserAdd(User user){
        boolean flag=us.addUser(user);
        CommonMessage cm=new CommonMessage();
        if(flag) {
            cm.setMsg("增加成功");
        }else {
            cm.setMsg("增加失败");}
        return cm;

    }

}
